app.controller('PopularModalCtrl', function ($scope, $modalInstance, toaster, param, ajax) {

	$scope.submitting = false;
	$scope.param = param;
	if ($scope.param.data == null) {
		$scope.param.data = {};
	}

	$scope.one = [];
	$scope.two = [];
	$scope.three = [];

	$scope.oneid = {};
	$scope.twoid = {};
	$scope.threeid = {};

	// $scope.popular = $scope.param.popu_list;
	$scope.sel = {};

	$scope.load = false;
	$scope.close = false;
	$scope.populars = "";

	ajax.call(new Req.GET({
		resource: 'pub/station/bk/' + $scope.param.data.id + '/popular'
	})).then(function (result) {
		if (result.code == 200 && result.result && result.result.length > 0) {
			var names = "";
			for (var idx in result.result) {
				names += result.result[idx].name;
				names += "、";
			}
			if (names.length > 0) {
				$scope.populars = names.substr(0, names.length - 1);
			}
			$scope.load = true;
		} else {
			$scope.close = true;
		}
	}, function () { });

	$scope.list = [];
	$scope.loadone = function () {
		var promise = ajax.call(new Req.GET({
			resource: 'pub/station/bk/' + param.data.id + '/terminal'
		}));
		promise.then(function (result) {
			$scope.one = result.result;
			$scope.two = angular.copy(result.result);
			$scope.three = angular.copy(result.result);
		}, function () { });
	};
	$scope.loadone();

	var inserter = new Req.POST({ resource: $scope.resource });

	$scope.ids = [];
	$scope.ok = function () {
		if ($scope.one.selected && $scope.two.selected && $scope.three.selected) {
			var one = $scope.one.selected.id;
			var two = $scope.two.selected.id;
			var three = $scope.three.selected.id;
			$scope.ids[0] = one;
			$scope.ids[1] = two;
			$scope.ids[2] = three;
			inserter.datalist = $scope.ids;
			inserter.resource = 'starting/bk/popular/' + param.data.id + '/insert';
			ajax.call(inserter).then(function (result) {
				if (result.code == 200) {
					toaster.pop('success', '成功', '添加热门站点成功。');
				} else {
					toaster.pop('error', '错误', '添加热门站点失败！');
				}
			}, function () {
				toaster.pop('error', '错误', '添加热门站点失败！');
			});
			$modalInstance.close();
		} else {
			toaster.pop('warning', '提示', '请选择三个站点！');
		}
	};

	$scope.cancel = function () {
		$modalInstance.dismiss('cancel');
		$scope.submitting = false;
	};

});